package ups.Analysis;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import ups.Common.DataImporter;
import ups.Common.Ship;
import ups.Database.ShipDatabase;
import ups.MapParse.Pixel;

public class LocationMapper extends DataImporter {
	public static int pixelsWide = 451;
	public static int pixelsHigh = 576;

	public static void main( String[] args ) {
		LocationMapper mapper = new LocationMapper( args );
	}
	
	public static Hashtable< String, Pixel > colors;
	
	public LocationMapper( String[] args ) {
		super( args );
		
		colors = new Hashtable<String, Pixel>();
		colors.put( "Wenatchee", Pixel.redPixel );
		colors.put( "Klahowya", Pixel.bluePixel );
		colors.put( "Kaleetan", Pixel.greenPixel );
		
		BufferedImage img = new BufferedImage( pixelsWide, pixelsHigh, BufferedImage.TYPE_INT_RGB );
		
		ShipDatabase shipDB = new ShipDatabase(  DBHostName ,DBPort, DBName, DBUsername, DBPass );
		
		
		Calendar date = Calendar.getInstance();
		date.set( Calendar.DAY_OF_YEAR, 306  );
		date.set( Calendar.YEAR, 2006 );
		
		for( String key : colors.keySet() ) {
			ArrayList<Ship> ships = shipDB.getShipsForDay(key, date.getTime() );
			System.out.println( ships.size() );
			for( Ship ship : ships ) {
				drawPixels( img, ship.getLocation().x, ship.getLocation().y, 1, 1, colors.get( key ).getIntValue() );
			}
		}
		
		System.out.println("Done drawing ships");
		try {
			ImageIO.write( img, "png", new File( "/Users/scanfield/Desktop/out.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	

	private void drawPixels( BufferedImage image, int x, int y, int width, int height, int color ) {
		System.out.printf( "Drawing rect at {%d,%d,%d,%d}\n", x,y,width,height);
		int i,j;
		for( i = x ; i < x+width; i++ ) {
			for( j = y; j < y+height; j++ ) {
				image.setRGB( i, j, color );
			}
		}
	}
	
	
}
